
/*This function is used to get the service number by account through ajax request and return the populated xml data*/
function getServiceByAccount(accountNumber){
    var myurl="getServiceByAccount.action";
    var mydata="account="+accountNumber;
    var serviceKeyNode= $("#serviceId");
    var changeMethod=$("#serviceId").attr("onchange");
    $.ajax({type:"POST", data:mydata, url:myurl,dataType:'xml',success:function(xml){
        if(serviceKeyNode[0].nodeName=='INPUT'){
            if($(xml).find("service").find("serviceId").length==1){
                $("#serviceId").val($(xml).find("service").find("serviceId").text());
                $("div",$("#serviceId").parent()).text($(xml).find("service").find("serviceNumber").text());
            }else{
                var serviceKeyParent = $("#serviceId").parent();
                if($("#serviceId")[0].tagName == "INPUT"){
                    $("#serviceId").prev().remove();
                    $("#serviceId").replaceWith("<select name='serviceId' id='serviceId'/>");
                    $("#serviceId").attr('onchange',changeMethod);
                }else{
                    serviceKeyParent.empty();
                }
                $(xml).find("service").each(function(){
                    var serviceKey = $(this).find("serviceId").text();
                    var serviceNumber = $(this).find("serviceNumber").text();
                    $("#serviceId").append("<option value="+serviceKey+">"+serviceNumber+"</option>");
                });
            }
        }else{
            if($(xml).find("service").size()<=1){
                $("#serviceId").replaceWith("<div class='labelbold'>"+$(xml).find("service").find("serviceNumber").text()+"</div><input type='hidden' value='"+$(xml).find("service").find("serviceId").text()+"' id='serviceId' name='serviceId'/>");
                $("#serviceId").attr('onchange',changeMethod);
            }else{
                $("#serviceId").replaceWith("<select name='serviceId' id='serviceId'/>");
                $("#serviceId").attr('onchange',changeMethod);
                $(xml).find("service").each(function(){
                    var serviceKey = $(this).find("serviceId").text();
                    var serviceNumber = $(this).find("serviceNumber").text();
                    $("#serviceId").append("<option value="+serviceKey+">"+serviceNumber+"</option>");

                });
            }
        }
    }});
}

function getServiceByAccountStatement(accountNumber,peroidKey,statementKey){
    var myurl="getServiceByAccountStatement.action";
    var mydata="account="+accountNumber+"&peroid="+peroidKey+"&statement="+statementKey;
    var serviceKeyNode= $("#serviceId");
    var changeMethod=$("#serviceId").attr("onchange");
    $.ajax({type:"POST", data:mydata, url:myurl,dataType:'xml',success:function(xml){
        if(serviceKeyNode[0].nodeName=='INPUT'){
            if($(xml).find("service").find("serviceId").length==1){
                $("#serviceId").val($(xml).find("service").find("serviceId").text());
                $("div",$("#serviceId").parent()).text($(xml).find("service").find("serviceNumber").text());
            }else{
                var serviceKeyParent = $("#serviceId").parent();
                if($("#serviceId")[0].tagName == "INPUT"){
                    $("#serviceId").prev().remove();
                    $("#serviceId").replaceWith("<select name='serviceId' id='serviceId'/>");
                    $("#serviceId").attr('onchange',changeMethod);
                }else{
                    serviceKeyParent.empty();
                }
                $(xml).find("service").each(function(){
                    var serviceKey = $(this).find("serviceId").text();
                    var serviceNumber = $(this).find("serviceNumber").text();
                    $("#serviceId").append("<option value="+serviceKey+">"+serviceNumber+"</option>");
                });
            }
        }else{
            if($(xml).find("service").size()<=1){
                $("#serviceId").replaceWith("<div class='labelbold'>"+$(xml).find("service").find("serviceNumber").text()+"</div><input type='hidden' value='"+$(xml).find("service").find("serviceId").text()+"' id='serviceId' name='serviceId'/>");
                $("#serviceId").attr('onchange',changeMethod);
            }else{
                $("#serviceId").replaceWith("<select name='serviceId' id='serviceId'/>");
                $("#serviceId").attr('onchange',changeMethod);
                $(xml).find("service").each(function(){
                    var serviceKey = $(this).find("serviceId").text();
                    var serviceNumber = $(this).find("serviceNumber").text();
                    $("#serviceId").append("<option value="+serviceKey+">"+serviceNumber+"</option>");

                });
            }
        }
        //get the statement number by statement data
        getStatementNumberByAccountNumber(accountNumber);
    }});
}

function getStatementNumberByAccountNumber(accountNumber){
    var tempurl="getStatementsByAccount.action";
    var tempdata="account="+accountNumber;
    $.ajax({type:"POST", data:tempdata, url:tempurl,dataType:'xml',success:function(xml){
    getStatementNumberByStatementDate(xml);
    }});
}
function getPremiseAddressByAccount(accountNumber,peroidKey,statementKey){
    var myurl="getPremiseAddressByAccount.action";
    var mydata="account="+accountNumber+"&peroid="+peroidKey+"&statement="+statementKey;
    var premiseKeyNode= $("#premiseKey");
    var changeMethod=$("#premiseKey").attr("onchange");
    $.ajax({type:"POST", data:mydata, url:myurl,dataType:'xml',success:function(xml){

        if(premiseKeyNode[0].nodeName=='INPUT'){
            if($(xml).find("premiseaddress").find("premisekey").length==1){
                $("#premiseKey").val($(xml).find("premiseaddress").find("premisekey").text());
                $("div",$("#premiseKey").parent()).text($(xml).find("premiseaddress").find("address").text());
            }else{
                var premiseKeyParent = $("#premiseKey").parent();
                if($("#premiseKey")[0].tagName == "INPUT"){
                    $("#premiseKey").prev().remove();
                    $("#premiseKey").replaceWith("<select name='premiseKey' id='premiseKey'/>");
                    $("#premiseKey").attr('onchange',changeMethod);
                }else{
                    premiseKeyParent.empty();
                }
                $(xml).find("premiseaddress").each(function(){
                    var premiseKey = $(this).find("premisekey").text();
                    var address = $(this).find("address").text();
                    $("#premiseKey").append("<option value="+premiseKey+">"+address+"</option>");
                });
            }
        }else{
            if($(xml).find("premiseaddress").size()<=1){
                $("#premiseKey").replaceWith("<div class='labelbold'>"+$(xml).find("premiseaddress").find("address").text()+"</div><input type='hidden' value='"+$(xml).find("premiseaddress").find("premisekey").text()+"' id='premiseKey' name='premiseKey'/>");
                $("#premiseKey").attr('onchange',changeMethod);
            }else{
                $("#premiseKey").replaceWith("<select name='premiseKey' id='premiseKey'/>");
                $("#premiseKey").attr('onchange',changeMethod);
                $(xml).find("premiseaddress").each(function(){
                    var premiseKey = $(this).find("premisekey").text();
                    var address = $(this).find("address").text();
                    $("#premiseKey").append("<option value="+premiseKey+">"+address+"</option>");

                });
            }
        }
        getStatementNumberByAccountNumber(accountNumber);
		isDisputeDisplay(xml);
    }});
}

//fix bug 16068382
function isDisputeDisplay(statementxml){
	if((document.forms['reportForm'].statementCombineKey.type=='select-one'&&document.forms['reportForm'].statementCombineKey.options[0].selected)||document.forms['reportForm'].statementCombineKey.type=='hidden'){				
		var disputeFlag = $(statementxml).find("disputeFlag").text();
		if(disputeFlag=="false"){
			$("#dispute").addClass("removebutton ");
		}else{
			$("#dispute").removeClass("removebutton");
		}		
	}else{
		$("#dispute").addClass("removebutton");
	}
}

function statementDispute(url)
{
	try{
		//url = url+"?accountId="+document.getElementById('accountId').value+"&periodKey="+document.getElementById('reportPeriodStart').value+"&statementFactTableId=2";
		var periodKey = "";
		var statementCombineKeyObj = document.getElementById("statementCombineKey");
		if(statementCombineKeyObj!=null) {
			var value = statementCombineKeyObj.value;
			var i = value.indexOf("|");
			periodKey = value.substring(i+1);
		}
		url = url + "?accountId=" + document.getElementById('accountId').value + "&periodKey=" + periodKey + "&statementFactTableId=2";
		popup(url);
	}catch(e){
		alert(' Unable to open up the page '+url);
	}
}

//move from unbilled.xml
function resetOperation(){
	document.forms['reportForm'].operation.value = "";
	return true;
}

function getServiceAgreementList(){
	
	<!-- same script is part of B2C also replicate any change for B2B -->
	document.forms['reportForm'].accountNo.value = document.forms['reportForm'].accountId.options[document.forms['reportForm'].accountId.selectedIndex].text;
	document.forms['reportForm'].action ='unBilledActvity.action';
	document.forms['reportForm'].operation.value = "ACNT_NUM_CHANGE";
	document.forms['reportForm'].submit();
}

	

//End of script